package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotUpkeepPlan;
import org.dromara.iot.domain.bo.IotUpkeepPlanBo;
import org.dromara.iot.domain.vo.IotUpkeepPlanVo;
import org.dromara.iot.mapper.IotUpkeepPlanMapper;
import org.dromara.iot.service.IIotUpkeepPlanService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 电梯保养计划Service业务层处理
 *
 * @author lsl
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class IotUpkeepPlanServiceImpl implements IIotUpkeepPlanService {

    private final IotUpkeepPlanMapper baseMapper;

    /**
     * 查询电梯保养计划
     */
    @Override
    public IotUpkeepPlanVo queryById(Long planId) {
        return baseMapper.selectVoById(planId);
    }

    /**
     * 查询电梯保养计划列表
     */
    @Override
    public TableDataInfo<IotUpkeepPlanVo> queryPageList(IotUpkeepPlanBo bo, PageQuery pageQuery) {
        QueryWrapper<IotUpkeepPlan> lqw = Wrappers.query();
        lqw.like(StringUtils.isNotBlank(bo.getElevatorName()), "ie.elevator_name", bo.getElevatorName());
        Page<IotUpkeepPlanVo> result = baseMapper.selectOwnVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯保养计划列表
     */
    @Override
    public List<IotUpkeepPlanVo> queryList(IotUpkeepPlanBo bo) {
        LambdaQueryWrapper<IotUpkeepPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotUpkeepPlan> buildQueryWrapper(IotUpkeepPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotUpkeepPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getElevatorId() != null, IotUpkeepPlan::getElevatorId, bo.getElevatorId());
        lqw.eq(bo.getTemplateId() != null, IotUpkeepPlan::getTemplateId, bo.getTemplateId());
        lqw.eq(StringUtils.isNotBlank(bo.getUpkeepType()), IotUpkeepPlan::getUpkeepType, bo.getUpkeepType());
        lqw.eq(bo.getUpkeepInterval() != null, IotUpkeepPlan::getUpkeepInterval, bo.getUpkeepInterval());
        lqw.eq(bo.getRemindDay() != null, IotUpkeepPlan::getRemindDay, bo.getRemindDay());
        return lqw;
    }

    /**
     * 新增电梯保养计划
     */
    @Override
    public Boolean insertByBo(IotUpkeepPlanBo bo) {
        IotUpkeepPlan add = MapstructUtils.convert(bo, IotUpkeepPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlanId(add.getPlanId());
        }
        return flag;
    }

    /**
     * 修改电梯保养计划
     */
    @Override
    public Boolean updateByBo(IotUpkeepPlanBo bo) {
        IotUpkeepPlan update = MapstructUtils.convert(bo, IotUpkeepPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotUpkeepPlan entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯保养计划
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public IotUpkeepPlanVo getByElevatorId(Long elevatorId) {
        LambdaQueryWrapper<IotUpkeepPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(IotUpkeepPlan::getElevatorId, elevatorId);
        return baseMapper.selectVoOne(lqw);
    }
}
